﻿using System;
using System.Text;
using System.Runtime.InteropServices.ComTypes;
using Nancy;
using Nancy.Responses;
using PlatWL.Models;
using PlatWL.Services;
using PlatWL.Data;

namespace PlatWL.Modules
{
    public abstract class SimpleModule : Nancy.NancyModule
    {
        protected Msg_Result msg = new Msg_Result() { };
        protected SimpleModule(string segment) : base("/api1/" + segment)
        {

            this.Before += ctx =>
            {
                if (this.Context != null && Request != null)
                {
                    msg.action = this.Request.Method + "  " + this.Request.Path;

                }
                return ctx.Response;
            };

            After += ctx =>
            {
                // Modify ctx.Response
            };
        }
    }

    #region 手机端接口路由机制
    public abstract class MsgModule : Nancy.NancyModule
    {
        protected Msg_Result msg = new Msg_Result() { };
        protected Msg_Page page = new Msg_Page() { };
        protected pt_user_account user_account = new pt_user_account();
        protected int Client { get; private set; }
        protected MsgModule(string segment)
            : base("/api/" + segment)
        {
            this.Before += ctx =>
            {
                if (this.Context != null && Request != null)
                {
                    msg.action = this.Request.Method + "  " + this.Request.Path;
                    //如果登录，获取登录用户信息
                    if (this.Context.CurrentUser != null)
                    {
                        var ue = (this.Context.CurrentUser as UserIdentity);
                        if (ue.UserName != null)
                        {
                            user_account = new pt_user_accountb().GetEntity(p => p.ID == ue.UserID);
                            //判断是否禁用
                            if (user_account.Status != 0 && user_account.Status == 9)
                            {
                                user_account = null;
                                msg.error = "没有权限";
                                return this.Response.AsJson(msg);
                            }

                        }

                    }

                    #region 获取分页参数,关键字搜索
                    var pageindex = this.Request.Query["pageindex"];
                    var pagesize = this.Request.Query["pagesize"];
                    if (!string.IsNullOrEmpty(pageindex))
                    {
                        int pIndex = 0;
                        if (Int32.TryParse(pageindex, out pIndex))
                        {
                            page.pageindex = pIndex;
                        }
                    }
                    if (!string.IsNullOrEmpty(pagesize))
                    {
                        int pSize = 0;
                        if (Int32.TryParse(pagesize, out pSize))
                        {
                            page.pagesize = pSize;
                        }
                    }
                    var key = this.Request.Query["key"];
                    if (!string.IsNullOrEmpty(key))
                    {
                        page.key = key;
                    }
                    #endregion
                }
                return ctx.Response;
            };

            After += ctx =>
            {
                // Modify ctx.Response
            };

        }

    }
    #endregion

    #region 管理员接口路由机制,所有后台接口以adapi开头
    public abstract class AdminModule : Nancy.NancyModule
    {
        protected Msg_Result msg = new Msg_Result() { };
        protected Msg_Page page = new Msg_Page() { };
        protected pt_auth_user auth_user = new pt_auth_user();
        protected int Client { get; private set; }
        protected AdminModule(string segment)
            : base("/adapi/" + segment)
        {
            this.Before += ctx =>
            {
                if (this.Context != null && Request != null)
                {
                    msg.action = this.Request.Method + "  " + this.Request.Path;

                    if (segment.ToLower() == "lg" || segment.ToLower() == "weixin") //登录不需要验证
                    {

                    }
                    else
                    {
                        //如果登录，获取登录用户信息
                        if (this.Context.CurrentUser != null)
                        {
                            var userid = (this.Context.CurrentUser as UserIdentity).UserID;
                            //Client = (this.Context.CurrentUser as UserIdentity).Client;
                            auth_user = new pt_auth_userb().GetEntity(p => p.ID == userid);
                        }
                        else
                        {
                            msg.error = "没有权限";
                            return this.Response.AsJson(msg);
                            //return new HtmlResponse(HttpStatusCode.Unauthorized);
                        }
                    }

                    #region 获取分页参数,关键字搜索
                    var pageindex = this.Request.Query["pageindex"];
                    var pagesize = this.Request.Query["pagesize"];
                    if (!string.IsNullOrEmpty(pageindex))
                    {
                        int pIndex = 0;
                        if (Int32.TryParse(pageindex, out pIndex))
                        {
                            page.pageindex = pIndex;
                        }
                    }
                    if (!string.IsNullOrEmpty(pagesize))
                    {
                        int pSize = 0;
                        if (Int32.TryParse(pagesize, out pSize))
                        {
                            page.pagesize = pSize;
                        }
                    }
                    var key = this.Request.Query["key"];
                    if (!string.IsNullOrEmpty(key))
                    {
                        page.key = key;
                    }
                    #endregion

                }

                //return (this.Context.CurrentUser == null) ?  HtmlResponse(HttpStatusCode.Unauthorized) : null;
                return ctx.Response;
            };

            After += ctx =>
            {
                // Modify ctx.Response
            };

        }

    }
    #endregion
}